package com.market.cash.ui.activity

import android.annotation.SuppressLint
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.GridLayoutManager
import com.market.cash.R
import com.market.cash.base.BaseFragment
import com.market.cash.databinding.CashMainBinding
import com.market.cash.pro.ProActivity
import com.market.cash.ui.adapter.CashRootMainTabAdapter
import com.market.cash.ui.presenter.CashRootMainPresenter
import com.market.cash.ui.view.ICashRootMainView
import com.market.cash.utils.adapter.CashRootOnItemChildClickListener
import com.market.cash.utils.event.CashRootEventData
import com.market.cash.utils.helper.CashRootCustomDataHelper
import com.market.cash.utils.log.CashRootLog
import org.json.JSONObject

/**
 * CashRoot
 * @Author wwdeng
 * @CreateTime: 2024/10/18 14:39
 * @Annotation：主页
 */
class MainActivity: ProActivity(), ICashRootMainView {
    lateinit var binding: CashMainBinding
    var presenter: CashRootMainPresenter? = null

    private var tabAdapter: CashRootMainTabAdapter? = null

    private var fragmentManager: FragmentManager? = null
    private var curtFragment = Fragment()
    override fun getCashRootRootView(): View {
        binding = CashMainBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initCashRootView() {
        setBackExit(true)
        initCashRootRecyclerView()
    }

    override fun initCashRootListener() {

    }

    override fun initCashRootData() {
        presenter = CashRootMainPresenter(this)
        showCashRootLoading()
        presenter!!.loadCashRootTabList()
        presenter!!.loadCashRootFragmentList()

        presenter!!.loadCashRootProcessAction()
        presenter!!.loadCashRootProductListAction()

        //加载保存自定义数据
        CashRootCustomDataHelper.loadCashRootCustomDataAction()
    }

    /**
     * 初始化Tab列表
     */
    private fun initCashRootRecyclerView() {
        binding.recyclerView.layoutManager = GridLayoutManager(getContext(), 4)
        tabAdapter = CashRootMainTabAdapter(binding.recyclerView)
        binding.recyclerView.adapter = tabAdapter
        binding.recyclerView.itemAnimator = DefaultItemAnimator()
        tabAdapter!!.setOnItemChildClickListener(CashRootOnItemChildClickListener { _, childView, position ->
            if (childView.id == R.id.itemLayout) {//切换item
                presenter!!.setCashRootCurrentItemIndex(position)
            }
        })
    }

    override fun showCashRootTabList(tabList: ArrayList<JSONObject>) {
        tabAdapter!!.data = tabList
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun notifyTabDataChanged() {
        tabAdapter!!.notifyDataSetChanged()
    }

    override fun showCashRootFragment(fragment: BaseFragment, index: Int) {
        if (fragmentManager == null) {
            fragmentManager = supportFragmentManager
        }
        val transaction = fragmentManager!!.beginTransaction()
        //如果之前没有添加过
        if (!fragment.isAdded) {
            //第三个参数为添加当前的fragment时绑定一个tag
            transaction.hide(curtFragment).add(R.id.contentLayout, fragment, fragment.javaClass.canonicalName)
        } else {
            transaction.hide(curtFragment).show(fragment)
            if (index == 0 || index == 1){
                presenter!!.loadCashRootProductListAction()
            }
        }
        curtFragment = fragment
        transaction.commitAllowingStateLoss()
    }

    override fun onCashRootEvent(event: CashRootEventData) {
        super.onCashRootEvent(event)
        CashRootLog.e(">>>>>>>>>>main>>>>>>>>>>>sub event："+event.eventType)
        when(event.eventType){
            CashRootEventData.TYPE.CASHROOT_PROCESS_REFRESH -> {//刷新流程
                presenter!!.loadCashRootProcessAction()
            }
            CashRootEventData.TYPE.CASHROOT_SHOW_HOME -> {//显示首页
                presenter!!.setCashRootCurrentItemIndex(0)
            }
            CashRootEventData.TYPE.CASHROOT_SHOW_REPAY -> {//待还款
                presenter!!.setCashRootCurrentItemIndex(1)
            }
            CashRootEventData.TYPE.CASHROOT_PRODUCT_REFRESH -> {//刷新产品
                presenter!!.loadCashRootProductListAction()
            }
            else -> {

            }
        }
    }
}